package com.tenement.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.tenement.dao.TenementClientMapper;
import com.tenement.dao.TenementLogMapper;
import com.tenement.dao.TenementTypeMapper;
import com.tenement.domain.TenementClient;
import com.tenement.domain.TenementLog;
import com.tenement.domain.TenementType;
import com.tenement.domain.TenementUser;
import com.tenement.service.TypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName : TypeServiceImpl  //类名
 * @Description : 户型服务接口实现类  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-07 20:38  //时间
 */
@Service
public class TypeServiceImpl implements TypeService {

    /**
     * 收藏dao
     */
    @Resource
    TenementTypeMapper typeMapper;

    /**
     * 客户dao
     */
    @Resource
    TenementClientMapper clientMapper;

    @Resource
    TenementLogMapper logMapper;

    /**
     * 查询收藏客户（条件，分页）
     * @param type
     * @param page
     * @param limit
     * @param startDate
     * @param endDate
     * @param userId
     * @return
     */
    @Override
    public List findAll(TenementClient type, Integer page, Integer limit, String startDate, String endDate,String userId) {
        if(clientTypes(userId).size()<1){
            return null;
        }
        PageHelper.startPage(page,limit);
        return clientMapper.selectAll(clientTypes(userId),type,startDate,endDate);
    }

    private List<String> clientTypes(String userId){
        List<TenementType> list= typeMapper.selectAll(userId,null);
        List<String> clientIds=new ArrayList<>();
        for (TenementType tenementType : list) {
            clientIds.add(tenementType.getTypeName());
        }
        return  clientIds;
    }

    /**
     * 查询收藏客户总行数
     * @param type
     * @param startDate
     * @param endDate
     * @param userId
     * @return
     */
    @Override
    public Long overCount(TenementClient type, String startDate, String endDate,String userId) {
        if(clientTypes(userId).size() < 1){
            return 0l;
        }
        return clientMapper.overCount(clientTypes(userId),type,startDate,endDate);
    }

    /*@Override
    public List<TenementType> selectAll() {
        return typeMapper.selectAll();
    }*/

    @Override
    public int update(TenementType type, TenementUser user, String ip) {
        type.setUpdatedBy(user.getUserName());
        type.setUpdatedTime(new Date());
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogIp(ip);
        log.setLogTitle("修改收藏客户信息");
        StringBuffer logBody=new StringBuffer();
        log.setLogBody(new Gson().toJson(type));
        log.setLogType(4);
        logMapper.insertSelective(log);
        return typeMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public int add(TenementType type, TenementUser user, String ip) {
        List<TenementType> tenementType=typeMapper.selectAll(user.getUserId(),type.getTypeName());
        if(tenementType.size()>0){
            return 0;
        }
        type.setTypeId(UUID.randomUUID().toString().replaceAll("-",""));
        type.setUserId(user.getUserId());
        type.setRevision(1);
        type.setCreatedBy(user.getUserName());

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogIp(ip);
        log.setLogTitle("新增收藏客户信息");
        log.setLogBody(new Gson().toJson(clientMapper.selectByPrimaryKey(type.getTypeName())));
        log.setLogType(1);
        logMapper.insertSelective(log);
        //type.setCreatedTime(new Date());
        return typeMapper.insert(type);
    }

    /**
     *
     * @param clientId
     * @param userId
     * @param ip
     * @return
     */
    @Override
    public int deleteById(String clientId, String userId, String ip) {
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogIp(ip);
        log.setLogTitle("删除收藏客户信息");
        log.setLogBody(new Gson().toJson(clientMapper.selectByPrimaryKey(clientId)));
        log.setLogType(2);
        logMapper.insertSelective(log);
        return typeMapper.deleteById(clientId,userId);
    }

    @Override
    public TenementType findById(String typeId) {
        return typeMapper.selectByPrimaryKey(typeId);
    }

    @Override
    public int deleteAll(String[] ids, String userId, String ip) {
        List<String> list=new ArrayList<>(Arrays.asList(ids));
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogIp(ip);
        log.setLogTitle("批量删除收藏客户信息");
        log.setLogBody(new Gson().toJson(clientMapper.findAllId(list)));
        log.setLogType(2);
        logMapper.insertSelective(log);

        return typeMapper.deleteAll(list,userId);
    }
}
